<template>
  <div id="sth-btn">
    <div class="wrap" v-show="isShowC">
      <div class="title">
        <span> Column Display </span>
        <span @click="resetTableHead"> Reset </span>
      </div>
      <div class="content">
        <draggable v-model="tableHeadC"  v-bind="dragOptions" draggable=".item" ghostClass="ghost" chosenClass="chosen"  @end="onEnd"  :sort= "true">
          <transition-group type="transition" name="flip-list">
            <template v-for="item in tableHeadC">
              <div :key="item.field_top" :class="[!item.disabled?'check-wrap item':'check-wrap']" v-if="type0(item)">
                <span>
                <i :class="[!item.disabled?'el-icon-rank':'']" ></i>
                  {{ item.field_value }}
                </span>
                <el-checkbox
                  v-model="item.fileld_state"
                  @change="isHeadShow"
                  :disabled="item.disabled"
                ></el-checkbox>
              </div>
            </template>
          </transition-group>
        </draggable>
        <!-- <template v-for="(item, index) in tableHeadC">
          <div :key="item + index" class="check-wrap" v-if="type0(item)">
            <span>
              {{ item.field_value }}
            </span>
            <el-checkbox
              v-model="item.fileld_state"
              @change="isHeadShow"
              :disabled="item.disabled"
            ></el-checkbox>
          </div>
        </template> -->
      </div>
    </div>
  </div>
</template>

<script>
import { set_table_head, reset_table_head } from "@/network/user/account";
import draggable from "vuedraggable";
export default {
  props: {
    isShow: {
      type: Boolean,
      default: () => false,
    },
    tableHead: {
      type: Array,
      default: () => [],
    },
  },
  components: {
    draggable,
  },
  data() {
    return {
      isShowC: false,
      tableHeadC: [],
      hasChange: false,
    };
  },
  computed: {
    dragOptions() {
      return {
        animation: 0,
        group: "description",
        disabled: false,
        ghostClass: "ghost"
      };
    }
  },
  watch: {
    isShow: {
      immediate: true,
      handler(val) {
        // console.log(val,'-------');
        this.isShowC = val;
      },
    },
    tableHead: {
      handler(val) {
        this.tableHeadC = [...val];
        // console.log(val,'-----------');
      },
      deep: true,
    },
  },
  methods: {
    /**
     * 拖拽结束事件
     */
    onEnd(e) {
      this.hasChange = true;
      this.$emit('changeOrder',{old:e.oldIndex,new:e.newIndex});
    },
    /**
     * 隐藏不需要展示的字段
     */
    type0(item) {
      // Object.prototype.hasOwnProperty.call(object, key)
      if (Object.prototype.hasOwnProperty.call(item, "value_type")) {
        return item.value_type;
      } else {
        return true;
      }
    },
    /**
     * 判断用户是否操作了列表
     */
    isHeadShow() {
      this.hasChange = true;
    },
    /**
     * 隐藏列表
     */
    hideMenu(e) {
      e.stopPropagation();
      let x = e.clientX;
      let y = e.clientY;
      if (document.querySelector("#sth-btn")) {
        let div = document.querySelector("#sth-btn");
        // console.log(x,y,div.offsetWidth,div.offsetHeight,div.offsetLeft,div.offsetTop);
        let divx1 = div.offsetLeft;
        let divy1 = div.offsetTop;
        let divx2 = div.offsetLeft + div.offsetWidth;
        let divy2 = div.offsetTop + div.offsetHeigth;
        if (this.isShow) {
          if (x < divx1 || x > divx2 || y < divy1 || y > divy2) {
            this.$emit("hideMenu");
            if (this.hasChange) {
              this.setTableHead();
            }
          }
        }
      }
    },
    /**
     * 重置列表
     */
    async resetTableHead() {
      let params = {
        data: {
          right_path: this.$route.path,
        },
      };
      await reset_table_head(params);
      this.hasChange = false;
      this.$emit("resetTableHead");
    },
    /**
     * 保存用户设置
     */
    async setTableHead() {
      let tempArr = [], table_detail_user_new_list = [];
      for (const item of this.tableHead) {
        // console.log(item);
        if (item.hasOwnProperty("fileld_state") && item.fileld_state) {
          // console.log(item.field_key);
          tempArr.push({
            field_key: item.field_key,
            table_set_id: item.table_set_id,
            field_top: item.field_top,
            field_width: item.field_width,
          });
        }
      }
      tempArr.forEach((item, index) => {
        
        item.field_top = index+1
        table_detail_user_new_list.push(item)
      });
      // console.log(table_detail_user_new_list);
      let params = {
        data: {
          table_detail_user_new_list,
        },
      };
      await set_table_head(params);
      this.hasChange = false;
    },
  },
  created() {
    this.isShowC = this.isShow;

    this.tableHeadC = [...this.tableHead];
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
    document.addEventListener("click", (e) => {
      this.hideMenu(e);
    });
    document.addEventListener("scroll", (e) => {
      this.$emit("hideMenu");
    });
  },
  destroyed() {
    document.removeEventListener("click", (e) => {
      this.hideMenu(e);
    });
    document.removeEventListener("scroll", (e) => {
      this.$emit("hideMenu");
    });
  },
};
</script>

<style lang="scss" scoped>
.ghost {
  opacity: 0.5;
  background: #F1E7DF;
}
.chosen {
  // background-color:#F1E7DF;

}
.item {
  cursor: move;
}
.flip-list-move {
  transition: transform 0.5s;
}
#sth-btn {
  position: fixed;
  // left: calc(100% - 264px);
  top: 250px;
  z-index: 2001;
  font-size: 12px;

  // i.sth-btn-icon:hover {
  //   cursor: pointer !important;
  // }
  .wrap {
    display: block;
    min-width: 220px;
    border: 1px solid #ccc;
    // padding: 10px;
    background-color: #fff;
    border-radius: 2px;
    box-sizing: border-box;
    .title {
      display: flex;
      justify-content: space-between;
      padding: 10px;
      span:last-child {
        color: #c95c3d;
        cursor: pointer;
      }
    }
    .content {
      padding: 0.6rem 14px;
      border-top: 1px solid #ccc;
      height: 170px;
      overflow: auto;
      min-width: 220px;
      .check-wrap {
        height: 1.7rem;
        line-height: 1.7rem;
        color: #333;
        display: flex;
        justify-content: space-between;
      }
    }
    &:before {
      box-sizing: content-box;
      width: 0px;
      height: 0px;
      position: absolute;
      top: -15px;
      right: 6px;
      padding: 0;
      border-bottom: 8px solid #ffffff;
      border-top: 8px solid transparent;
      border-left: 8px solid transparent;
      border-right: 8px solid transparent;
      display: block;
      content: "";
      z-index: 12;
    }
    &:after {
      box-sizing: content-box;
      width: 0px;
      height: 0px;
      position: absolute;
      top: -17px;
      right: 5px;
      padding: 0;
      border-bottom: 9px solid #cccccc;
      border-top: 9px solid transparent;
      border-left: 9px solid transparent;
      border-right: 9px solid transparent;
      display: block;
      content: "";
      z-index: 10;
    }
  }
}
</style>